<?php
    require_once('popup_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class popup_manage_won extends popup_base_jectly {

        protected $contents_html='<div class=\'center\'>This is popup_manage_won page.</div>';

        protected $id;//project_id
        public $project;
        protected $won_id;//won_id
        public $won;//this can be unique only by project id but will check from query string too

        function __construct() {
            parent::__construct();
        }

        protected function Form_Create() {

            $this->check_query_string();

            $this->load_project();
		}

        protected function check_query_string()
        {
            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }

            if(is_numeric(QApplication::QueryString('wid')))
            {
                $this->won_id=QApplication::QueryString('wid');//won_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
                $this->project=TableProject::LoadById($this->id);

                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.']';
                }
                else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
            }
        }

        //check permissions
        public function bool_project_loaded()
        {
            return isset($this->project);
        }

        public function bool_my_project()
        {
            if($this->project && isset($this->UserId))
            {
                return $this->project->bool_my_project($this->UserId);
            }

            return false;
        }

        public function bool_won_project()
        {
            if($this->project && isset($this->UserId))
            {
                return $this->project->bool_won_project($this->UserId);
            }

            return false;
        }

        protected function bool_already_bidding()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_bidding_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }

        protected function bool_already_won()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_won_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }

        public function bool_project_open()
        {
            if($this->project && isset($this->UserId))
            {
                return $this->project->bool_open_project();
            }

            return false;
        }

        public function bool_project_after_picked()
        {
            if($this->project && isset($this->UserId))
            {
                return $this->project->bool_after_picked_project();
            }

            return false;
        }

        public function bool_project_bidding_end()
        {
            if(!bool_project_open() && !bool_project_after_picked())return true;

            return false;
        }

        public function bool_manage_won_permitted()
        {
            if(!$this->bool_logined())return false;
            if(!$this->bool_project_loaded())return false;
            if($this->bool_my_project())return false;
            if(!$this->bool_project_open())return false;
            
            if(TableProejct::bool_can_manage_won_for_the_project($this->UserId,$this->id))
            {
                return true;
            }
            
            return false;
        }

        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
            }
        }

	}

    popup_manage_won::Run('popup_manage_won','popup_manage_won.tpl.php');

?>